# Copyright (c) Microsoft. All rights reserved.
# Licensed under the MIT license. See LICENSE.md file in the project root for full license information.

# <code>
## 输入文件text.txt/ssml文件
## 输出文件file.wav

from azure.cognitiveservices.speech import AudioDataStream, SpeechSynthesizer
from azure.cognitiveservices.speech.audio import AudioOutputConfig
import azure.cognitiveservices.speech as speechsdk

speech_key, service_region = "f8bf74d4723845a19e385418bb0a1642", "eastasia"
speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
speech_config.set_speech_synthesis_output_format(speechsdk.SpeechSynthesisOutputFormat.Audio24Khz48KBitRateMonoMp3)

synthesizer = SpeechSynthesizer(speech_config=speech_config)

# text = "你吃饭了吗？我现在声音自然多了吧"
# with open('text.txt', 'r',encoding='utf-8',errors='ignore') as f:
   # text = f.read()

ssml_string = open("apply/speech/ssml.xml", "r",encoding='UTF-8').read()
# SSML中需要书签标签，例如。
# ssml = "<speak version='1.0' xml:lang='zh-CN' xmlns='http://www.w3.org/2001/10/synthesis' xmlns:mstts='http://www.w3.org/2001/mstts'><voice name='en-US-JennyNeural'><mstts:express-as style="sad" styledegree="2"><bookmark mark='bookmark_one'/><p><s>Introducing the sentence element.</s><s>Used to mark individual sentences.</s></p><p>Another simple paragraph.Sentence structure in this paragraph is not explicitly marked.</p></voice></mstts:express-as></speak>";

# result = synthesizer.speak_text_async(text).get() #这个是普通文本
result = synthesizer.speak_ssml_async(ssml_string).get() #这个是SSML文本
stream = AudioDataStream(result)
stream.save_to_wav_file("apply/speech/file.mp3")
